module(..., package.seeall)
local storyboard = require "storyboard"
local widget = require "widget"
function new(score, numberStar,arrayScore,onReplayEvent)
	print(numberStar)
	local ui = require("ui")
	local wEasing = require "wEasing"
	-- local pupGroup = display.newGroup()
	local gui = require "popup.popupWinGui".new()
	local popupWinGroup = display.newGroup()
	local arrayStar = {}
	local arrayLevel = {}
	local timerViewStar
	local txtScore 
	local keyStart = false
	local key1, key2, key3
	local bg = display.newImageRect(popupWinGroup, gui.bg.fn, gui.bg.w, gui.bg.h)
		bg.x = gui.bg.x
		bg.y = gui.bg.y
	local yourScore = display.newImageRect(popupWinGroup, gui.yourScore.fn, gui.yourScore.w, gui.yourScore.h)
		yourScore.x = gui.yourScore.x
		yourScore.y = gui.yourScore.y
		
	a = 0
	for i = 1,5 do
				arrayStar[i] = display.newImageRect(gui.noStar.fn, gui.noStar.w, gui.noStar.h)
				arrayStar[i].x = gui.noStar.x+a
				arrayStar[i].y = gui.noStar.y
				a = a+ 58
				popupWinGroup:insert(arrayStar[i])
	end
	a = 0
	txtScore = display.newText(popupWinGroup, "", gui.txtScore.x, gui.txtScore.y, gui.txtScore.font, gui.txtScore.size)
		txtScore.text = score
		txtScore:setTextColor(unpack(gui.txtScore.color))
		txtScore:setReferencePoint(display.CenterReferencePoint)
	if numberStar == 0 then
		key1 = ui.newButton({ 
									defaultSrc = gui.key1.fn,
									overSrc = gui.key1.fn,
									defaultX = gui.key1.w,
									defaultY = gui.key1.h,
									overX = gui.key1.w,
									overY = gui.key1.h,
									x = gui.key1.x,
									y = gui.key1.y,
									overAlpha = gui.key1.overAlpha,
									overScale = gui.key1.overScale,
									onRelease = function()
									end
								})
			popupWinGroup:insert(key1)
			key2 = ui.newButton({ 
									defaultSrc = gui.key2.fn,
									overSrc = gui.key2.fn,
									defaultX = gui.key2.w,
									defaultY = gui.key2.h,
									overX = gui.key2.w,
									overY = gui.key2.h,
									x = gui.key2.x,
									y = gui.key2.y,
									overAlpha = gui.key2.overAlpha,
									overScale = gui.key2.overScale,
									onRelease = function()
									end
								})
			popupWinGroup:insert(key2)
			key3 = ui.newButton({ 
									defaultSrc = gui.key3.fn,
									overSrc = gui.key3.fn,
									defaultX = gui.key3.w,
									defaultY = gui.key3.h,
									overX = gui.key3.w,
									overY = gui.key3.h,
									x = gui.key3.x,
									y = gui.key3.y,
									overAlpha = gui.key3.overAlpha,
									overScale = gui.key3.overScale,
									onRelease = function()
									end
								})
			popupWinGroup:insert(key3)
	end
	for i = 1,5 do
		arrayLevel[i] = display.newText(popupWinGroup, "",gui.txtLevel.x +a, gui.txtLevel.y, gui.txtLevel.font, gui.txtLevel.size)
		arrayLevel[i]:setTextColor(unpack(gui.txtLevel.color))
		a = a+58
		if i == 1 then
			arrayLevel[i].text = arrayScore[1]
		elseif i == 2 then
			arrayLevel[i].text = arrayScore[2]
		elseif i == 3 then
			arrayLevel[i].text = arrayScore[3]
		elseif i == 4 then
			arrayLevel[i].text = arrayScore[4]
		else
			arrayLevel[i].text = arrayScore[5]
		end
	end
	
	local btnHome = ui.newButton({ 
									defaultSrc = gui.btnHome.fn,
									overSrc = gui.btnHome.fn,
									defaultX = gui.btnHome.w,
									defaultY = gui.btnHome.h,
									overX = gui.btnHome.w,
									overY = gui.btnHome.h,
									x = gui.btnHome.x,
									y = gui.btnHome.y,
									overAlpha = gui.btnHome.overAlpha,
									overScale = gui.btnHome.overScale,
									onRelease = function()
													if keyStart == true then
													-- transition.to(popupWinGroup,{time = 100,alpha = 1, x = 0, y = -600, transition = wEasing.easeOutElastic})
													popupWinGroup:removeSelf()
													storyboard.gotoScene("Home.Home", "fade",10 )
													print ("move")
													end
													----------tro ve man hinh Home-------------------------
												end
												--onReplayEvent()
									})
		popupWinGroup:insert(btnHome)
	local btnReplay = ui.newButton({ 
									defaultSrc = gui.btnReplay.fn,
									overSrc = gui.btnReplay.fn,
									defaultX = gui.btnReplay.w,
									defaultY = gui.btnReplay.h,
									overX = gui.btnReplay.w,
									overY = gui.btnReplay.h,
									x = gui.btnReplay.x,
									y = gui.btnReplay.y,
									overAlpha = gui.btnReplay.overAlpha,
									overScale = gui.btnReplay.overScale,
									onRelease = function()
													if keyStart == true then
													transition.to(popupWinGroup,{time = 100,alpha = 1, x = 0, y = -500, transition = wEasing.easeOutElastic,
														onComplete = function()
																		popupWinGroup:removeSelf()
																	end})
													--------------------choi lai tu dau----------------------------
													onReplayEvent()
													end
												end
									})
		popupWinGroup:insert(btnReplay)
		
timeStar = (numberStar+1)*500
function timeViewStar()
    timeStar = timeStar - 500
    if timeStar ==0 then
		keyStart = true
     timer.cancel(timerViewStar)
    else
     if timeStar == numberStar*500 then
      arrayStar[1] = display.newImageRect(gui.star.fn, gui.star.w, gui.star.h)
      arrayStar[1].x = gui.star.x
      arrayStar[1].y = gui.star.y
      popupWinGroup:insert(arrayStar[1])
     end
	 if timeStar == (numberStar-1)*500 then
      arrayStar[2] = display.newImageRect(gui.star.fn, gui.star.w, gui.star.h)
      arrayStar[2].x = gui.star.x+58
      arrayStar[2].y = gui.star.y
      popupWinGroup:insert(arrayStar[2])
     end
     if timeStar == (numberStar-2)*500 then
      arrayStar[3] = display.newImageRect(gui.star.fn, gui.star.w, gui.star.h)
      arrayStar[3].x = gui.star.x+2*58
      arrayStar[3].y = gui.star.y
      popupWinGroup:insert(arrayStar[3])
     end
     if timeStar == (numberStar-3)*500 then
      arrayStar[4] = display.newImageRect(gui.star.fn, gui.star.w, gui.star.h)
      arrayStar[4].x = gui.star.x+3*58
      arrayStar[4].y = gui.star.y
      popupWinGroup:insert(arrayStar[4])
     end
     if timeStar == (numberStar-4)*500 then
      arrayStar[5] = display.newImageRect(gui.star.fn, gui.star.w, gui.star.h)
      arrayStar[5].x = gui.star.x+4*58
      arrayStar[5].y = gui.star.y
      popupWinGroup:insert(arrayStar[5])
     end
    end
	end
	timerViewStar = timer.performWithDelay(700,timeViewStar,0)
	return popupWinGroup
end